function table = syndtable(h)
//SYNDTABLE Produce syndrome decoding table.
//   T = SYNDTABLE(H) returns a binary matrix that represents the first column of
//   a standard array of a linear code having a parity-check matrix H.  T
//   consists of coset leaders ordered sequentially by the associated syndromes
//   such that the first row contains an error pattern with a syndrome equal to
//   0, and the last row contains an error pattern with a syndrome equal to
//   2^(number of rows in H)-1.

if isempty(h)
    error('The parity check matrix cannot be empty.');
end

[n_k,n]=size(h);                    // n_k denotes n-k
k = n - n_k;
if n_k >= n | ~isequal(size(size(h)),[1 2]),
    error('There must be more columns than rows in the parity check matrix.');
end

if  max(h) < 0 | max(h) > 1 | int(h) ~= h then
    error('The parity check matrix must contain only binary numbers.');
end

table = zeros(2^n_k,n);
emptyRows = [2:2^n_k]';
ht = h';

errBitNum = 1;
while errBitNum < n & ~isempty(emptyRows)
  errPositions = nchoosek(1:n, errBitNum);
  errPatternNum = cnk(n, errBitNum);
  for i = 1:errPatternNum
    err = zeros(1,n);
    err(errPositions(i, :)) = 1;
    tableIndex = bi2de(fliplr(modulo(err*ht,2))) + 1;
    j = find(emptyRows == tableIndex, 1);
    if j ~= []
      table(tableIndex,:) = err;
      emptyRows(j) = [];
    end
  end
  disp([string(errBitNum) '-error patterns loaded.  ' string(length(emptyRows)) ' rows remaining.']);
  errBitNum = errBitNum + 1;
end

endfunction
